package com.jcl.cloud.services.crawler.brain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jcl.cloud.services.brain.common.dto.BuyResumeDataModel;
import com.jcl.cloud.services.crawler.brain.task.WorkerService;
import com.jcl.cloud.services.gateway.common.task.BuyResumeReq;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class GrabBuyResumeWorkerService implements WorkerService {
    private static final Logger logger = LoggerFactory.getLogger(GrabBuyResumeWorkerService.class);


    @Autowired
    private RedisService redisService;

    @Override
    public Object doWorker(String traceId) {
        BuyResumeDataModel buyResumeDataModel = null;
        try {
            String buyResumeData = this.redisService.rpop("unicrawler:buy-resume:tasks");

            logger.info("获取到的购买信息为：" + buyResumeData);
            if(StringUtils.isNotBlank(buyResumeData)){
                JSONObject jo = JSON.parseObject(buyResumeData);

                JSONObject dataJO = jo.getJSONObject("data");

                String crawlerId = jo.getString("crawlerId");
                String taskIdForBuy = jo.getString("taskIdForBuy");
                String tenantId = jo.getString("tenantId");


                BuyResumeReq buyResumeReq = JSON.toJavaObject(dataJO,BuyResumeReq.class);




               // Map map = JSON.toJavaObject(JSON.parseObject(buyResumeData),HashMap.class);
                buyResumeDataModel = new BuyResumeDataModel();

                buyResumeDataModel.setTaskIdForBuy(taskIdForBuy);
                buyResumeDataModel.setCrawlerId(crawlerId);
                buyResumeDataModel.setTenantId(tenantId);
                //BuyResumeReq buyResumeReq = JSON.toJavaObject(JSON.parseObject((String) map.get("data")),BuyResumeReq.class);
                buyResumeDataModel.setBuyResumeContext(buyResumeReq.getBuyResumeContext());
                buyResumeDataModel.setTaskType(buyResumeReq.getTaskType());
                buyResumeDataModel.setSiteCode(buyResumeReq.getSiteCode());

            }
        } catch (Exception e) {
           logger.error("获取购买任务发生异常；" + e.getMessage(),e);
        }

        return buyResumeDataModel;
    }
}
